const app = getApp();
Page({

  data: {
    formData: {
      deviceCode: '3402 0000 0032 0000 0193',
      password: '123456',
      serverIP: '203.56.2.108',
      serverPort: '5060',
      deviceLocation: '万达广场',
      serverCode: '340200000131000001',
      saveDays: '30',
      streamParma: '21250',
    },
    markerImg: app.getImgUrl('location.svg'),
    loadingCircleImg: app.getImgUrl('loadingCircle.webp'),
    showDrawer: false,
    currentStep: 0,
    expand: false,
    resultSuccess: false,
    secList: [
      { id: 10, name: "爱尔幼儿园", selected: true },
      { id: 9, name: "蓝天幼稚园", selected: false },
      { id: 8, name: "七色花幼儿园", selected: false },
      { id: 7, name: "小天使幼稚园", selected: false },
      { id: 6, name: "红黄蓝幼儿园", selected: false },
      { id: 5, name: "金宝幼稚园", selected: false },
      { id: 4, name: "青苗幼儿园", selected: false },
      { id: 3, name: "彩虹星幼稚园", selected: false },
      { id: 2, name: "智慧星幼儿园", selected: false },
      { id: 1, name: "常青藤幼稚园", selected: false }
    ]
  },
  onLoad() { },
  toggleDrawer() {
    // 直接调用组件方法（需获取实例）
    const drawer = this.selectComponent('#v-drawer-log')
    drawer.toggleDrawer()
  },
  toggleExpand() {
    this.setData({ expand: this.data.expand ? false : true });
  },
  handleSelectItem(e) {
    const selectedId = e.currentTarget.dataset.id; // 获取点击项的id
    const newList = this.data.secList.map(item => {
      return {
        ...item,
        selected: item.id === selectedId // 当前项设为true，其他自动变false
      };
    });
    this.setData({ secList: newList }); // 更新数据驱动视图
  },
  nextStep() {
    this.setData({ currentStep: this.data.currentStep + 1 });
    if (this.data.currentStep == 2) {
      setTimeout(() => {
        this.setData({ currentStep: this.data.currentStep + 1 });
        this.renewResult()
      }, 5000);
    }
  },
  handleCustomBack() {
    let content = this.data.currentStep === 0 ? '确定放弃接入并返回？' : '返回上一步？'
    wx.showModal({
      title: '提示',
      content: content,
      success: (res) => {
        if (res.confirm) {
          if (this.data.currentStep == 0) {
            wx.navigateBack({ delta: 1 })
          } else {
            this.setData({ currentStep: this.data.currentStep - 1 });
            if (this.data.currentStep == 2) {
              setTimeout(() => {
                this.setData({ currentStep: this.data.currentStep + 1 });
                this.renewResult()
              }, 5000);
            }
          }
        }
      }
    })
  },
  returnModify() {
    this.setData({ currentStep: 0 });
    this.renewResult()
  },
  renewResult() {
    const result = Math.random() > .5 ? true : false;
    this.setData({ resultSuccess: result });
  },
  navigateTo(e){
    app.navigateTo(e.currentTarget.dataset.url,e.currentTarget.dataset.type)
  }
})